<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>今日番茄 Ttomato.</title>
    <link href="favicon.ico" type="image/x-icon" rel="shortcut icon">
    <link rel="stylesheet" type="text/css" href="components.css" />
    <link rel="stylesheet" type="text/css" href="kityminder.editor.min.css">
    <style>
      html, body {
        margin: 0;
        padding: 0;
        height: 100%;
        overflow: hidden;
      }
      
      div.minder-editor-container {
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
      }
    </style>
  </head>

  <body ng-app="kityminderDemo" ng-controller="MainController">
    <kityminder-editor on-init="initEditor(editor, minder)"></kityminder-editor>
  </body>
  <script type="application/javascript" src="components.js"></script>
  <script type="application/javascript" src="kityminder.editor.min.js"></script>
  <script>
    function getUrlParam(name) {
      //构造一个含有目标参数的正则表达式对象  
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      //匹配目标参数  
      var r = window.location.search.substr(1).match(reg);
      //返回参数值  
      if (r != null) return unescape(r[2]);
      return null;
    }
    angular.module('kityminderDemo', ['kityminderEditor'])
      .config(function(configProvider) {
        configProvider.set('imageUpload', '../server/imageUpload.php');
      })
      .controller('MainController', function($scope) {
        $scope.initEditor = function(editor, minder) {
          const mid = getUrlParam("mid");
          window.editor = editor;
          window.minder = minder;
          const pdoc = window.parent.document;
          const kity = pdoc.getElementById(`kity-${mid}`);
          window.document.addEventListener("keydown", (event) => {
            if (event.ctrlKey && event.keyCode === 83) {
              event.preventDefault();
              const evt = pdoc.createEvent("UIEvents");
              evt.initUIEvent("force-synchronize", true, true);
              kity.dispatchEvent(evt);
            };
          }, true);
          pdoc.addEventListener("keydown", (event) => {
            if (event.ctrlKey && event.keyCode === 83) {
              event.preventDefault();
              const evt = pdoc.createEvent("UIEvents");
              evt.initUIEvent("force-synchronize", true, true);
              kity.dispatchEvent(evt);
            };
          });
          minder.on('contentchange', () => {
            const evt = pdoc.createEvent("UIEvents");
            evt.initEvent("auto-synchronize", true, true);
            kity.dispatchEvent(evt);
          });
          const evt = pdoc.createEvent("UIEvents");
          evt.initUIEvent("enable-editor", true, true);
          evt.editor = editor;
          kity.dispatchEvent(evt);
        };
      });
  </script>

</html>